package com.wisemoresystem.seal.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_check;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_handle;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章不符
 * @author hxj
 *
 */
@Service
public class SealErrorService extends BaseService {
	
	@Autowired
	private VerifyConfigService configService;
	@Autowired
	private SealNewService sNewService;
	@Autowired
	private SealSetService setService;
	
	@Task(code = "seal_distribution7", tip = "未完成的印章不符处理(@cnt)")
	public long selectDistributeRecieveTwiceList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.flag_stop='暂时停用' and t.flag='印章出错' ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId(),WebTools.getCurrentUserId());
		
		return cnt;
	}
	
	//不符列表
	public List<Map<String, Object>> selectSealErrorList(String bank_id,String srh_name,Page page){
		
		String sql="select t.*,tb.name bank_name,tn.title,tc.name config_name from t_seal t "
				+ " left join t_seal_new tn on t.seal_new_id=tn._id "
				+ "	left join t_seal_type_config tc on tn.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='暂时停用' and t.save_user_id=? "
				+ " and t.flag='印章出错' and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(), page);
		return list;
	}

	//处理
	@Transactional
	public boolean error(String seal_id,t_seal_handle handle) {
		
		t_seal seal = super.findObjById(seal_id, t_seal.class);
		seal.setFlag("待上缴");
		seal.setFlag_stop("上缴停用");
		super.updateObj(seal);
		
		handle.setBank_id(WebTools.getCurrentBankId());
		handle.setUser_id(WebTools.getCurrentUserId());
		handle.setApply_time(new Timestamp(System.currentTimeMillis()));
		super.saveObj(handle);
		
		mySubmit(handle);
		return true;
	}
	
	@Transactional
	private void mySubmit(t_seal_handle handle) {

		boolean isVerify = configService.isGetUnMatchHandleStart();
		if (isVerify == true) {
			t_check check = new t_check();
			check.setBank_id(handle.getBank_id());
			check.setType("不符处理复核");
			check.setIs_verify("是");
			check.setVerify("待审核");
			check.setUser_id(WebTools.getCurrentUserId());
			super.saveObj(check);

			handle.setCheck_id(check.get_id());
			handle.setFlag("待复核");
			super.updateObj(handle);
		}else{
			handle.setFlag("通过");
			super.updateObj(handle);
			
			t_seal seal = super.findObjById(handle.getSeal_id(), t_seal.class);
			
			checkOverLevel(seal);
			
			if("重新刻章".equals(handle.getHandle_type())){
				sNewService.reSealNew(seal.getSeal_new_id());
			}
		}
	}
	
	//判断为上缴，还是直接销毁
	private t_seal checkOverLevel(t_seal seal){
		t_bank bank = super.findObjById(WebTools.getCurrentBankId(),
				t_bank.class);
		seal.setFlag_stop("上缴停用");
		seal.setUse_bank_id(WebTools.getCurrentBankId());
		
		//判断盖章是否在销毁权限之上
		if (setService.checkIsTop(seal.get_id())) {
			seal.setFlag("待销毁");
			seal.setUse_bank_id(bank.getSuper_id());
		} else {
			seal.setFlag("停用");
		}
		return seal;
	}
		
	
	@Task(code = "seal_distribution8", tip = "未完成的印章不符复核(@cnt)")
	public long selectSealErrorCheckList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_handle th on th.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and t.flag_stop='上缴停用' and t.flag='待上缴' and th.flag='待复核' ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId(),WebTools.getCurrentUserId());
		return cnt;
	}
	
	//复核列表
	public List<Map<String, Object>> selectSealErrorCheckList(String bank_id,String srh_name,Page page){
	
		String sql="select t.*,tb.name bank_name,tn.title,tc.name config_name,th._id handle_id,th.handle_type from t_seal t "
				+ " join t_seal_handle th on th.seal_id=t._id "
				+ " left join t_seal_new tn on t.seal_new_id=tn._id "
				+ "	left join t_seal_type_config tc on tn.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='上缴停用' and t.save_user_id!=? "
				+ " and t.flag='待上缴' and t.true_code like ? and th.flag='待复核' order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(), page);
		return list;
	}

	//复核
	@Transactional
	public boolean check(String handle_id, t_check check) {

		t_seal_handle handle = super.findObjById(handle_id, t_seal_handle.class);
		handle.setFlag(check.getVerify());
		super.updateObj(handle);
		
		t_check tc = super.findObjById(handle.getCheck_id(), t_check.class);
		tc.setVerify(check.getVerify());
		tc.setVerify_bank_id(WebTools.getCurrentBankId());
		tc.setVerify_time(new Timestamp(System.currentTimeMillis()));
		tc.setVerify_user_id(WebTools.getCurrentUserId());
		tc.setNote(check.getNote());
		super.updateObj(tc);
		
		t_seal seal = super.findObjById(handle.getSeal_id(), t_seal.class);
		
		if("通过".equals(handle.getFlag())){
			
			checkOverLevel(seal);
			
			if("重新刻章".equals(handle.getHandle_type())){
				sNewService.reSealNew(seal.getSeal_new_id());
			}
		}else{
			seal.setFlag("停用");
			seal.setFlag_stop("暂时停用");
		}
		super.updateObj(seal);
		
		return true;
	}
	
}
